package payrechargelogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeTypeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeTypeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeTypeListLogic {
	return &RechargeTypeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// RechargeTypeList 充值类型查询
func (l *RechargeTypeListLogic) RechargeTypeList(in *rpc.RechargeTypeListRequest) (*rpc.RechargeTypeListResponse, error) {
	var commonResp rpc.CommonResp
	db := model.NewRechargeTypeModel(l.ctx, l.svcCtx.Db)

	var (
		types []schema.RechargeType
		err   error
	)

	if in.Name != "" {
		types, err = db.GetRechargeTypesByName(in.Name)
	} else {
		types, err = db.GetAllRechargeTypes()
	}

	if err != nil {
		commonResp.Code = xerrors.DBError
		commonResp.Message = "Database operation error"
		return &rpc.RechargeTypeListResponse{
			CommonResp: &commonResp,
		}, err
	}

	var records []*rpc.RechargeTypeList
	for _, t := range types {
		records = append(records, &rpc.RechargeTypeList{
			Id:             t.ID,
			CreatedAt:      t.CreatedAt,
			UpdatedAt:      t.UpdatedAt,
			DeletedAt:      0,
			RechargeId:     t.RechargeID,
			SelectedIcon:   t.SelectedIcon,
			UnselectedIcon: t.UnselectedIcon,
			Name:           t.Name,
			InternalName:   t.InternalName,
			Status:         uint32(t.Status),
			Sort:           t.Sort,
			ViewType:       t.ViewType,
			IsCopyIcon:     int32(t.IsCopyIcon),
		})
	}

	return &rpc.RechargeTypeListResponse{
		Records:    records,
		CommonResp: &commonResp,
	}, nil
}
